package com.yiku.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author:ludada
 * @Date:2022/11/23 13:04
 * 元对象处理器
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    //插入时的填充策略
    @Override
    public void insertFill(MetaObject metaObject) {
//        log.info("start insert fill........");
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sf.format(new Date());
        this.setFieldValByName("gmtCreate",format,metaObject);
        this.setFieldValByName("gmtModified",format,metaObject  );
        this.setFieldValByName("informTime",format,metaObject  );
        this.setFieldValByName("version",0,metaObject);
        this.setFieldValByName("deleted",0,metaObject);

    }
    //更新时的填充策略
    @Override
    public void updateFill(MetaObject metaObject) {
//        log.info("start update fill....");
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sf.format(new Date());
        this.setFieldValByName("gmtModified",format,metaObject  );
    }
}
